package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Person;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("findPage")
    public Page findPage(@RequestParam(required = true,defaultValue = "1") Integer pageNum,@RequestBody Person person){
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(person.getName()!=null,"name",person.getName());
        queryWrapper.like(person.getAddress()!=null,"address",person.getAddress());

        Page<Person> page1=new Page(pageNum,3);
        Page<Person> personPage=personService.findPage(page1,queryWrapper);
        return personPage;

    }

    @PostMapping("add")
    public void add(@RequestBody Person person){
        //person.setId(0L);
        personService.add(person);
    }


    @PostMapping("update")
    public void update(@RequestBody Person person){
        personService.update(person);
    }

    @GetMapping("findById")
    public Person findById(int id){
         Person person= personService.findById(id);
         return person;
    }

    @GetMapping("del")
    public void del(int id){
        personService.del(id);
    }
}
